package com.vvlhw.pptmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.vvlhw.pptmall.product.entity.GoodsEntity;
import com.vvlhw.pptmall.product.service.GoodsService;
import com.vvlhw.common.utils.PageUtils;
import com.vvlhw.common.utils.R;



/**
 * 商品表
 *
 * @author lianghanwei
 * @email 15362941982@163.com
 * @date 2020-06-14 16:11:50
 */
@RestController
@RequestMapping("product/goods")
@Api(tags = "商品操作")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 查询所有商品，逻辑删除、非上架的不查
     * 给前端商城
     */
    @ApiOperation("查询所有上架商品信息")
    @GetMapping("/listOnSale")
    public R listOnSale(){
        List<GoodsEntity> list = new LambdaQueryChainWrapper<>(goodsService.getBaseMapper()).eq(GoodsEntity::getOnSale, 1).list();
        return R.ok().put("listOnSale", list);
    }


    /**
     * 查询所有商品，只限定了逻辑删除的不查
     * 给后端管理系统
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("查询单个商品接口")
    @GetMapping("/info/{gid}")
    public R info(@ApiParam("写个2，下面Execute，观察")@PathVariable("gid") Long gid){
		GoodsEntity goods = goodsService.getById(gid);

        return R.ok().put("goods", goods);
    }

    /**
     * 模糊查询：根据分类id and goodEntity的所有字段
     */
    @ApiOperation("查询接口，可模糊查询")
    @GetMapping("/search")
    public R search(@ApiParam("写个3，对应[小清新]类别")@RequestParam(value = "catId", required = false) Long catId, @ApiParam("可写可不写，不写即只按分类查")@RequestParam(value = "other", required = false) String other){
        List<GoodsEntity> list = goodsService.getBycatIdAndOther(catId, other);
        return R.ok().put("goods", list);
//        if (catId == null) {
//            System.out.println("catId是null");
//        } else {
//            System.out.println(catId);
//        }
//        if (other == null) {
//            System.out.println("other是null");
//        } else {
//            System.out.println(other);
//        }
//        return R.ok().put("test", "" + catId + " " + other);
    }


    /**
     * 保存
     */
//    @ApiOperation("新增商品接口")
    @PostMapping("/mySave")
    public R mySave(@RequestBody GoodsEntity goods){
//		goodsService.save(goods);
        Long insertGid = goodsService.mySave(goods);
        return R.ok().put("insertGid", insertGid);
    }

    /**
     * 修改
     */
//    @ApiOperation("修改商品接口")
    @PostMapping("/update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);
        return R.ok();
    }

    /**
     * 【分类维护】 可以修改分类名称
     * 这里为了减少查询，增加了一个分类名称catName的冗余字段
     * 因此我这里得给一个接口让其保持同步
     * 只需要一个catId、修改的分类名称catName
     */
    @PostMapping("/updateCatName/{catId}/{catName}")
    @Transactional
    public R updateCatName(@PathVariable("catId")Long catId, @PathVariable("catName")String catName){
//        goodsService.updateById(goods);
        goodsService.updateCatNameByCatId(catId, catName);
        return R.ok();
    }

    /**
     * 删除
     */
//    @ApiOperation("删除商品接口，可批量")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] gids){
		goodsService.removeByIds(Arrays.asList(gids));

        return R.ok();
    }

}
